﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.Components;
using LaughingDog.Input;
using LaughingDog.GameObjects;
using System.Reflection;

namespace LaughingDog.Scripts
{
    public class EnableScriptOnClick : Script
    {
        private Type mScriptTypeToEnable = null;        
        private String mScriptName = "";
        private MouseButtons? mMouseButton = MouseButtons.LeftButton;
        private Boolean mToggleEnabling = false; // TODO Fix the double-calling of mouse event delagates as it currently breaks the toggling.

        /// <summary>
        /// 
        /// </summary>
        public Type ScriptTypeToEnable { get { return mScriptTypeToEnable; } set { mScriptTypeToEnable = value; } }

        /// <summary>
        /// Gets or sets a value indicating the name of the script to enable. If blank then the first found script of the specified type will be used.
        /// </summary>
        public String ScriptName { get { return mScriptName; } set { mScriptName = value; } }

        /// <summary>
        /// Gets or sets a value indicating the MouseButton to use to enable the script. Default is LeftButton. If set to null then will be enabled on the press of any mouse button.
        /// </summary>
        public MouseButtons? MouseButton { get { return mMouseButton; } set { mMouseButton = value; } }

        /// <summary>
        /// Creates a new instance of a EnableScriptOnClick script component.
        /// </summary>
        /// <param name="gameObject">The GameObject that this component should be attached to.</param>
        public EnableScriptOnClick(GameObject gameObject)
            : base(gameObject)
        {
            GameObject.OnMouseJustReleased += OnMouseJustReleased;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="button"></param>
        public void OnMouseJustReleased(MouseButtons button)
        {
            if (mScriptTypeToEnable != null)
            {
                Script foundScript = null;

                if (mScriptName.Equals(""))
                {
                    MethodInfo[] methods = typeof(GameObject).GetMethods();

                    foreach (MethodInfo info in methods)
                    {
                        if (info.Name == "GetComponent")
                        {
                            if (info.IsGenericMethod)
                            {
                                if (info.GetParameters().Length == 0)
                                {
                                    MethodInfo genericMethod = info.MakeGenericMethod(info.GetGenericArguments());

                                    //Script script = (Script)genericMethod.


                                }
                            }
                        }
                    }
                    //Type constructedClass = mScriptTypeToEnable.MakeGenericType();

                   // object created = Activator.CreateInstance(constructedClass);
                }
                else
                    foundScript = GameObject.GetComponent<TurnToFacePoint>(mScriptName);

                if (foundScript != null)
                {
                    if (mMouseButton == null) // If mouse button is null then just activate the script regardless of what button was pressed.
                    {
                        performEnabling(foundScript);
                    }
                    else if (mMouseButton.Equals(button))
                    {
                        performEnabling(foundScript);
                    }
                }
            }
        }

        private void performEnabling(Script script)
        {
            if (mToggleEnabling)
                script.Enabled = !script.Enabled;
            else
                script.Enabled = true;
        }
    }
}
